/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataanalysis.gui.filechooser;

import java.awt.Component;
import java.awt.HeadlessException;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import weka.gui.ExtensionFileFilter;

/**
 *
 * @author Administrator
 */
public class ModelFileChooser extends JFileChooser {

    public ModelFileChooser(File currentDirectory) {
        super(currentDirectory);
        FileFilter modelFilter = new ExtensionFileFilter(".model", "Model object files (*.model)");
        this.setFileFilter(modelFilter);
        this.setFileSelectionMode(JFileChooser.FILES_ONLY);
        this.setMultiSelectionEnabled(false);
    }

    @Override
    public int showSaveDialog(Component parent) throws HeadlessException {
        int result = super.showSaveDialog(parent);
        if ((result == APPROVE_OPTION) && (getSelectedFile().exists())) {
            int retVal = JOptionPane.showConfirmDialog(
                    parent,
                    "The file '"
                    + getSelectedFile()
                    + "' already exists - overwrite it?");
            if (retVal == JOptionPane.OK_OPTION) {
                result = APPROVE_OPTION;
            } else if (retVal == JOptionPane.NO_OPTION) {
                result = showSaveDialog(parent);
            } else {
                result = CANCEL_OPTION;
            }
        }
        return result;
    }
}
